#include <func.h>
#include "ds.h"
#include "transMessage.h"
#include "tcpClientInit.h"
#include "inputAnalyze.h"
#include "epoll.h"
#include "puts_gets.h"

int main(int argc, char *argv[])
{
    ARGS_CHECK(argc, 3);
    int sockfd;
    int ret = tcpClientInit(&sockfd, argv[1], argv[2]);
    ERROR_CHECK(ret, -1, "tcpClientInit");

    int epfd = epoll_create(1);
    ret = epollAdd(epfd, sockfd);
    ERROR_CHECK(ret, -1, "epollAdd");

    ret = epollAdd(epfd, STDIN_FILENO);
    ERROR_CHECK(ret, -1, "epollAdd");
    char virtualPath[256] = {0};
    char realLocalPath[256] = {0};
    char buf[4096];
    train_t trainMsg;
    bool loginFlag = false;
    userinfo_t userinfo;
    printf("please input username and password , use a space separated.\n");
    while (1)
    {
        
        bzero(buf, sizeof(buf));
        ssize_t sret = read(STDIN_FILENO, buf, sizeof(buf));
        buf[sret-1]='\0';
        if (loginFlag == false)
        {
            bzero(&userinfo, sizeof(userinfo));
            ret = userinfoAnalyze(buf, &userinfo);
            if (ret == 1)
            {
                printf("login fail\n");
                continue;
            }
            transUserinfo(sockfd, &trainMsg, &userinfo);
        }
        bzero(&trainMsg, sizeof(trainMsg));
        sret = recv(sockfd, &trainMsg, sizeof(trainMsg), 0);
        if (trainMsg.flag == ISRESULT)
        {
            printf("%s", trainMsg.data);
        }
        else if (trainMsg.flag == ISLOGIN)
        {
            loginFlag = true;
            printf("%s", trainMsg.data);
            break;
        }
    }

    while (1)
    {
        struct epoll_event readySet[2];
        int readyNum = epoll_wait(epfd, readySet, 2, -1);
        for (int i = 0; i < readyNum; ++i)
        {

            if (readySet[i].data.fd == STDIN_FILENO)
            {

                bzero(buf, sizeof(buf));
                ssize_t sret = read(STDIN_FILENO, buf, sizeof(buf));
                buf[sret - 1] = '\0';
                printf("buflength=%ld\n", strlen(buf));

                cmd_t currentCmd;
                bzero(&currentCmd, sizeof(currentCmd));
                ret = commandAnalyze(buf, &currentCmd);
                if (ret == 1)
                {
                    continue;
                }
                transCommand(sockfd, &trainMsg, &currentCmd);

                if (currentCmd.cmdType == ISPUTS)
                {
                    bzero(realLocalPath, sizeof(realLocalPath));
                    bzero(virtualPath, sizeof(virtualPath));
                    memcpy(realLocalPath, currentCmd.parameter1, strlen(currentCmd.parameter1));
                    printf("realLocalPath=%s\n", realLocalPath);
                    memcpy(virtualPath, currentCmd.parameter2, strlen(currentCmd.parameter2));
                    printf("virtualPath=%s\n", virtualPath);
                    putsfiles(realLocalPath, sockfd, virtualPath);
                }
                if (currentCmd.cmdType == ISGETS)
                {
                    bzero(realLocalPath, sizeof(realLocalPath));
                    bzero(virtualPath, sizeof(virtualPath));
                    memcpy(virtualPath, currentCmd.parameter1, strlen(currentCmd.parameter1));
                    memcpy(realLocalPath, currentCmd.parameter2, strlen(currentCmd.parameter2));
                    printf("virtualPath=%s\n", virtualPath);
                    printf("realLocalPath=%s\n", realLocalPath);
                    getsfiles(realLocalPath, sockfd);
                }
            }
            else
            {
                bzero(&trainMsg, sizeof(trainMsg));
                ssize_t sret = recv(sockfd, &trainMsg, sizeof(trainMsg), 0);
                if (sret == 0)
                {
                    break;
                }
                if (trainMsg.flag == ISFILE)
                {
                    getsfiles(realLocalPath, sockfd);
                    printf("realLocalPath=%s\n", realLocalPath);
                }
                else if (trainMsg.flag == ISRESULT)
                {
                    printf("%s", trainMsg.data);
                }
            }
        }
    }
    return 0;
}
